//
//@File    :	14.15_stcktp1.h
//@Time    :	2022/02/23 16:42:35
//@Auther  :	Sun Sheng
//@Contact :	970340843@qq.com
//@Desc    :	modified Stack template
//

#ifndef STCKTP1_H_
#define STCKTP1_H_

template <typename T>
class Stack
{
private:
    enum{SIZE = 10};
    int stacksize;
    T *items;
    int top;

public:
    explicit Stack(int ss=SIZE);
    Stack(const Stack & st);
    ~Stack(){delete [] items;}
    bool isempty(){return top==0;}
    bool isfull(){return top == stacksize;}
    bool push(const T &item);
    bool pop(T & item);
    Stack &operator=(const Stack & st);
};

template<class T>
Stack<T>::Stack(int ss):stacksize(ss),top(0)
{
    items = new T[stacksize];
}

template<class T>
Stack<T>::Stack(const Stack & st)
{
    stacksize=st.stacksize;
    top=st.top;
    items=new T[stacksize];
    for (int i = 0; i < top; i++)
    {
        items[i]=st.items[i];
    }
}

template<class T>
bool Stack<T>::push(const T &item)
{
    if (top<stacksize){
        items[top++]=item;
        return true;
    }else{
        return false;
    }
}

template<class T>
bool Stack<T>::pop(T &item)
{
    if(top>0)
    {
        item=items[top--];
        return true;
    }else{
        return false;
    }
}


template<class T>
Stack<T> &Stack<T>::operator=(const Stack<T> & st){
    if (this == &st)
    {
        return *this;
    }
    delete [] items;//释放旧的栈
    stacksize = st.stacksize;
    top=st.top;
    items=new T[stacksize];//为新的栈分配内存
    for (int i = 0; i < top; i++)
    {
        items[i]=st.items[i];//将旧的复制到新的上
    }
    return *this;
}
#endif	// STCKTP1_H_